package sk.sigp.aobot.swinggui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

import sk.sigp.aobot.Colors;
import sk.sigp.aobot.Connect;
import sk.sigp.aobot.Utils;
import sk.sigp.aobot.base.MessageAdapter;
import sk.sigp.aobot.base.StaticUtils;
import sk.sigp.aobot.client.BotCore;
import sk.sigp.aobot.swinggui.utils.HyperTextListener;

class GroupFrame extends JFrame {

	private static final long serialVersionUID = 5843732353735896130L;
	private String group = null;
	private JEditorPane area;
	private String txt = "";
	private Connect connect;
	private MainFrame mainFrame;
	private JPanel downPanel;
	private JButton sendMessageButton;
	private JTextField messageTextField;

	private ActionListener sendMessageListener;
	
	private MessageAdapter messageAdapter;
	
	public MessageAdapter getMessageAdapter() {
		if (messageAdapter==null) {
			messageAdapter = new MessageAdapter() {
				
				@Override
				public void messageRecieved(final String message, String group) {
//					System.out.println("GF"+message);
					if (group.equals(getGroup())) { //just if our and message groups matchs
						SwingUtilities.invokeLater(new Runnable() {
							public void run() {
								String date = StaticUtils.actualTime();
								txt = "<font color=" + Utils.colorToRgb(Colors.FOREGROUND)
										+ ">" + txt + date + message + "</font><br/>";
								getArea().setText("<html><body>" + txt + "</body></html>");
								getArea().repaint();
							}
						});
					}			
				}
				
			};			
		}
		return messageAdapter;
	}
	
	public ActionListener getSendMessageListener() {
		if (sendMessageListener==null) {
			sendMessageListener = new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if (getConnect().getCore().isConnected()) {
						if (getMessageTextField().getText().length()>0) {
							Connect.scriptGroupTell(getGroup(), getMessageTextField().getText());
							getMessageTextField().setText("");
						}
					} else
						getMessageAdapter().messageRecieved("cant send message, not connected",getGroup());
				}
			};
		}
		return sendMessageListener;
	}

	
	public JTextField getMessageTextField() {
		if (messageTextField==null) {
			messageTextField=new JTextField();
			messageTextField.setBackground(Colors.BACKGROUND);
			messageTextField.setForeground(Colors.FOREGROUND);
			messageTextField.setCaretColor(Colors.CARET);
			messageTextField.setPreferredSize(new Dimension(300, 30));
			messageTextField.addActionListener(getSendMessageListener());
		}
		return messageTextField;
	}

	public JButton getSendMessageButton() {
		if (sendMessageButton==null) {
			sendMessageButton=new JButton("send");
			sendMessageButton.addActionListener(getSendMessageListener());
		}
		return sendMessageButton;
	}

	public JPanel getDownPanel() {
		if (downPanel==null) {
			downPanel= new JPanel();
			downPanel.setMinimumSize(new Dimension(0, 50));
			downPanel.add(getMessageTextField());
			downPanel.add(getSendMessageButton());
		}
		return downPanel;
	}

	private JEditorPane getArea() {
		if (area==null) {
			area = new JEditorPane();
			area.setEditable(false);
			area.setContentType("text/html");
			area.setBackground(Colors.BACKGROUND);
			area.setForeground(Colors.FOREGROUND);
	
			area.addHyperlinkListener(new HyperTextListener(getConnect(),getMainFrame()));
		}
		return area;
	}

	public GroupFrame(String group,Connect connect,MainFrame mainFrame) {
		setTitle("OestRC - "+group);
		setGroup( group );
		setConnect(connect);
		this.mainFrame = mainFrame;
		JPanel o = new JPanel();
		o.setLayout(new BorderLayout());
		setContentPane(o);
		setSize(700, 500);
		setLocation((int) Toolkit.getDefaultToolkit().getScreenSize()
				.getWidth()
				/ 2 - getWidth() / 2, (int) Toolkit.getDefaultToolkit()
				.getScreenSize().getHeight()
				/ 2 - getHeight() / 2);
		getContentPane().add(new JScrollPane(getArea()), BorderLayout.CENTER);
		getContentPane().add(getDownPanel(), BorderLayout.SOUTH);

		//add and remove message listeners
		getConnect().getCore().addListener(getMessageAdapter());
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				getConnect().getCore().removeListener(getMessageAdapter());
			}
		});
	}

	public BotCore getBotCore() {
		return getConnect().getCore();
	}

	public Connect getConnect() {
		return connect;
	}

	public void setConnect(Connect connect) {
		this.connect = connect;
	}

	public MainFrame getMainFrame() {
		return mainFrame;
	}

	public void setMainFrame(MainFrame mainFrame) {
		this.mainFrame = mainFrame;
	}

	public String getGroup() {
		return group;
	}

	public void setGroup(String group) {
		this.group = group;
	}
}
